//
//  CZFowardController.swift
//  GZWeibo666
//
//  Created by mac－gt on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD
//  枚举 用来标示是转发还是评论
enum CZCommentType{
    case CZCommentTypeSend
    case CZCommentTypeTalk
}
class CZFowardController: UIViewController {
    var status: CZStatus?
    private var type:CZCommentType
    init(type:CZCommentType,status:CZStatus?){
        self.type=type
        self.status=status
        super.init(nibName: nil, bundle: nil)
        //
      view.backgroundColor = UIColor.whiteColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let str = type == CZCommentType.CZCommentTypeSend ? "转发":"评论"
         setUpNav(str)
        setOtherView()
    
    }
    //  设置navigationItem,并添加点击事件
    func setUpNav(title:String){
    navigationController?.navigationBar.backgroundColor=UIColor.orangeColor()
        navigationItem.leftBarButtonItem=UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        navigationItem.rightBarButtonItem=UIBarButtonItem(title:title, style: UIBarButtonItemStyle.Plain, target: self, action: "forwordClick")
    }
    //  当点击或者转发、评论成功后要关闭当前控制器
    func close(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    //
    func forwordClick(){
        let content=felidView.text
        if content==nil{
           SVProgressHUD.showErrorWithStatus("内容为空")
        return
        }
        if type==CZCommentType.CZCommentTypeSend{
            SVProgressHUD.showErrorWithStatus("正在转发微博...", maskType: SVProgressHUDMaskType.Black)
            CZNetworkTool.sharedInstance.sendStatus((status?.id)!,content:content,finished: { (result, error) -> () in
                if error != nil{
                    SVProgressHUD.showErrorWithStatus("转发失败")
                    print("\(error)")
                    return
                }
                SVProgressHUD.showErrorWithStatus("转发成功")
                self.close()
            })
        }
        else if type==CZCommentType.CZCommentTypeTalk{
//            SVProgressHUD.showErrorWithStatus("正在评论微博", maskType: SVProgressHUDMaskType.Black)
            SVProgressHUD.showInfoWithStatus("正在评论微博", maskType: SVProgressHUDMaskType.Black)
            NSThread.sleepForTimeInterval(1)
           CZNetworkTool.sharedInstance.pingLunStatus((status?.id)!, comment:content, finished: { (result, error) -> () in
            if error != nil{
                SVProgressHUD.showErrorWithStatus("评论失败")
                return
            }
            SVProgressHUD.showInfoWithStatus("评论成功")
        
            self.close()
           })
        }
    }
    // 
    func setOtherView(){
        var title="转发微博"
        if type == CZCommentType.CZCommentTypeTalk{
            title="评论微博"
        }
        self.title=title
        view.addSubview(felidView)
        felidView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: view, size: UIScreen.mainScreen().bounds.size)
    }
    private lazy var felidView:UITextView={
        let view1=UITextView()
        view1.backgroundColor=UIColor.grayColor()
        view1.font=UIFont.systemFontOfSize(16)
        return view1
    }()
}
